import { defHttp } from '@/utils/http/axios';
import { Iresult } from '#/axios';

enum Api {
  // GetMenuList = 'Roles/permissions/resources',
  GetMenuList = '/Roles/permissions/resources',
  SwitchVue3Menu = '/sys/switchVue3Menu',
}

/**
 * @description: Get user menu based on id
 */

export const getMenuList = () => {
  return new Promise((resolve) => {
    //为了兼容mock和接口数据
    defHttp.get<Iresult>({ url: Api.GetMenuList }).then((res) => {
      if (Array.isArray(res.data)) {
        const resp = res.data;
        itemMeta(resp);
        resolve(resp);
      } else {
        resolve(res['menu']);
      }
    });
  });
};

function itemMeta(arr: any) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i].children.length > 0) {
      itemMeta(arr[i].children);
    }
    arr[i] = {
      ...arr[i],
      path: arr[i].url || '',
      title: arr[i].name,
      meta: {
        ...arr[i].meta,
        title: arr[i].name,
        name: arr[i].name,
        icon: arr[i].icon,
      },
    };
  }
}
